package com.example.shangyf.scene;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.shangyf.scene.bean.BackgroundEventsBean;
import com.example.shangyf.scene.bean.CoordinatesBean;
import com.example.shangyf.scene.bean.SceneBean;
import com.example.shangyf.scene.bean.ShopBackgroundTemplateBean;
import com.example.shangyf.scene.bean.ShopNavigationsBean;
import com.example.shangyf.scene.event.BubbleCoordinateEvent;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    SceneView sceneView;


    String json = "{\"shopBackgroundTemplate\":{\"backgroundHeight\":2000,\"backgroundImage\":\"http://img.ai3010.com/third/20181112/1542005164553CLeb.png\",\"backgroundWidth\":1500,\"created\":\"2018-11-12 14:51:38\",\"backgroundEvents\":[{\"event\":0,\"activation\":1,\"coordinates\":[{\"x\":978,\"y\":908,\"z\":0},{\"x\":1095,\"y\":969,\"z\":0},{\"x\":1101,\"y\":1022,\"z\":0},{\"x\":1121,\"y\":1033,\"z\":0},{\"x\":1124,\"y\":1167,\"z\":0},{\"x\":1014,\"y\":1217,\"z\":0},{\"x\":867,\"y\":1138,\"z\":0},{\"x\":869,\"y\":1012,\"z\":0},{\"x\":892,\"y\":989,\"z\":0},{\"x\":897,\"y\":952,\"z\":0}],\"textCoordinates\":[{\"x\":920,\"y\":914,\"z\":0}]},{\"event\":0,\"activation\":0,\"coordinates\":[{\"x\":1017,\"y\":1295,\"z\":0},{\"x\":1175,\"y\":1384,\"z\":0},{\"x\":1242,\"y\":1427,\"z\":0},{\"x\":1232,\"y\":1494,\"z\":0},{\"x\":1112,\"y\":1563,\"z\":0},{\"x\":952,\"y\":1475,\"z\":0},{\"x\":946,\"y\":1421,\"z\":0}],\"textCoordinates\":[{\"x\":978,\"y\":1327,\"z\":0}]},{\"event\":0,\"activation\":2,\"coordinates\":[{\"x\":667,\"y\":1092,\"z\":0},{\"x\":671,\"y\":1134,\"z\":0},{\"x\":778,\"y\":1194,\"z\":0},{\"x\":822,\"y\":1174,\"z\":0},{\"x\":825,\"y\":1228,\"z\":0},{\"x\":837,\"y\":1242,\"z\":0},{\"x\":841,\"y\":1348,\"z\":0},{\"x\":678,\"y\":1414,\"z\":0},{\"x\":496,\"y\":1300,\"z\":0},{\"x\":517,\"y\":1166,\"z\":0}],\"textCoordinates\":[{\"x\":600,\"y\":1091,\"z\":0}]},{\"event\":0,\"activation\":5,\"coordinates\":[{\"x\":1102,\"y\":398,\"z\":0},{\"x\":1154,\"y\":467,\"z\":0},{\"x\":1182,\"y\":475,\"z\":0},{\"x\":1172,\"y\":564,\"z\":0},{\"x\":1228,\"y\":577,\"z\":0},{\"x\":1232,\"y\":753,\"z\":0},{\"x\":1105,\"y\":826,\"z\":0},{\"x\":1067,\"y\":809,\"z\":0},{\"x\":1049,\"y\":814,\"z\":0},{\"x\":981,\"y\":772,\"z\":0},{\"x\":977,\"y\":753,\"z\":0},{\"x\":977,\"y\":582,\"z\":0},{\"x\":994,\"y\":562,\"z\":0},{\"x\":1025,\"y\":578,\"z\":0},{\"x\":1031,\"y\":502,\"z\":0},{\"x\":1019,\"y\":494,\"z\":0},{\"x\":1022,\"y\":471,\"z\":0},{\"x\":1046,\"y\":472,\"z\":0}],\"textCoordinates\":[{\"x\":1049,\"y\":427,\"z\":0}]},{\"event\":0,\"activation\":3,\"coordinates\":[{\"x\":452,\"y\":451,\"z\":0},{\"x\":558,\"y\":511,\"z\":0},{\"x\":578,\"y\":503,\"z\":0},{\"x\":646,\"y\":545,\"z\":0},{\"x\":652,\"y\":650,\"z\":0},{\"x\":680,\"y\":667,\"z\":0},{\"x\":555,\"y\":742,\"z\":0},{\"x\":320,\"y\":610,\"z\":0},{\"x\":341,\"y\":602,\"z\":0},{\"x\":343,\"y\":523,\"z\":0},{\"x\":360,\"y\":507,\"z\":0},{\"x\":370,\"y\":457,\"z\":0},{\"x\":371,\"y\":451,\"z\":0},{\"x\":417,\"y\":469,\"z\":0}],\"textCoordinates\":[{\"x\":335,\"y\":445,\"z\":0}]},{\"event\":0,\"activation\":4,\"coordinates\":[{\"x\":363,\"y\":762,\"z\":0},{\"x\":346,\"y\":776,\"z\":0},{\"x\":342,\"y\":788,\"z\":0},{\"x\":374,\"y\":795,\"z\":0},{\"x\":422,\"y\":886,\"z\":0},{\"x\":466,\"y\":921,\"z\":0},{\"x\":457,\"y\":936,\"z\":0},{\"x\":462,\"y\":1052,\"z\":0},{\"x\":347,\"y\":1118,\"z\":0},{\"x\":231,\"y\":1055,\"z\":0},{\"x\":224,\"y\":1029,\"z\":0},{\"x\":203,\"y\":1010,\"z\":0},{\"x\":234,\"y\":949,\"z\":0},{\"x\":229,\"y\":932,\"z\":0},{\"x\":214,\"y\":931,\"z\":0},{\"x\":230,\"y\":907,\"z\":0},{\"x\":235,\"y\":851,\"z\":0},{\"x\":277,\"y\":857,\"z\":0},{\"x\":308,\"y\":806,\"z\":0}],\"textCoordinates\":[{\"x\":259,\"y\":815,\"z\":0},{\"x\":351,\"y\":965,\"z\":0}]}],\"name\":\"二狗\",\"id\":\"8\",\"defaultTemplate\":true},\"shopNavigations\":[{\"activation\":2,\"content\":\"吼吼哈\",\"created\":\"2018-11-09 17:13:47\",\"id\":\"5\",\"lastUpdate\":\"2018-11-09 17:13:47\",\"shopId\":\"97320892\",\"shopName\":\"二狗火锅城\"},{\"activation\":1,\"content\":\"呵呵球2\",\"created\":\"2018-11-09 16:59:07\",\"id\":\"3\",\"lastUpdate\":\"2018-11-09 16:59:07\",\"shopId\":\"97320892\",\"shopName\":\"二狗火锅城\"},{\"activation\":0,\"content\":\"飞流直下三千尺，疑是银河落九天\",\"created\":\"2018-11-09 14:14:48\",\"id\":\"2\",\"lastUpdate\":\"2018-11-09 14:14:48\",\"shopId\":\"97320892\",\"shopName\":\"飞流直下三千尺，疑是银河落九天\"}]}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sceneView = findViewById(R.id.scene_view);
        sceneView.setOnSceneClickListener(new SceneView.OnSceneClickListener() {
            @Override
            public void OnSceneClick(int activation) {

            }
        });


        final SceneBean sceneBean = new Gson().fromJson(json, SceneBean.class);
        final ShopBackgroundTemplateBean shopBackgroundTemplateBean = sceneBean.getShopBackgroundTemplate();
        SimpleTarget<Bitmap> simpleTarget = new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                List<ShopNavigationsBean> list = sceneBean.getShopNavigations();
                List<BackgroundEventsBean> eventsBeanList = shopBackgroundTemplateBean.getBackgroundEvents();
                //点击区域
                List<BubbleCoordinateEvent> pathCoordinateList = new ArrayList<>();
                for (int i = 0; i < eventsBeanList.size(); i++) {
                    BackgroundEventsBean backgroundEventsBean = eventsBeanList.get(i);
                    BubbleCoordinateEvent bubbleCoordinateEvent = new BubbleCoordinateEvent();
                    bubbleCoordinateEvent.setActivation(backgroundEventsBean.getActivation());
                    bubbleCoordinateEvent.setCoordinatesBeanList(backgroundEventsBean.getCoordinates());
                    pathCoordinateList.add(bubbleCoordinateEvent);
                }
                //气泡区域的坐标
                List<BubbleCoordinateEvent> bubbleCoordinateList = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    ShopNavigationsBean shopNavigationsBean = list.get(i);
                    for (int j = 0; j < eventsBeanList.size(); j++) {
                        BackgroundEventsBean backgroundEventsBean = eventsBeanList.get(j);
                        if (shopNavigationsBean.getActivation() == backgroundEventsBean.getActivation()) {
                            BubbleCoordinateEvent bubbleCoordinateEvent = new BubbleCoordinateEvent();
                            bubbleCoordinateEvent.setActivation(shopNavigationsBean.getActivation());
                            bubbleCoordinateEvent.setContent(shopNavigationsBean.getContent());
                            bubbleCoordinateEvent.setCoordinatesBeanList(backgroundEventsBean.getTextCoordinates());
                            bubbleCoordinateList.add(bubbleCoordinateEvent);
                        }
                    }
                }
                //获取手指的坐标
                int handX = -1;
                int handY = -1;
                if (eventsBeanList != null) {
                    BackgroundEventsBean backgroundEventsBean = eventsBeanList.get(eventsBeanList.size() - 1);
                    if (backgroundEventsBean != null) {
                        List<CoordinatesBean> coordinatesList = backgroundEventsBean.getTextCoordinates();
                        if (coordinatesList != null && coordinatesList.size() > 1) {
                            CoordinatesBean coordinatesBean = coordinatesList.get(1);
                            handX = coordinatesBean.getX();
                            handY = coordinatesBean.getY();
                        }
                    }
                }

                sceneView.setSceneBitmap(bitmap, pathCoordinateList, bubbleCoordinateList, handX, handY);
                sceneView.start();
            }
        };
        //"http://img.ai3010.com/third/20181112/1542005164553CLeb.png"
        Glide.with(this)
                .asBitmap()
                .load(shopBackgroundTemplateBean.getBackgroundImage())
                .into(simpleTarget);

    }
}
